﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace AMebooking.Models
{
    public class Reservation
    {
        public const string STATUS_WAITING = "Waiting"; //time not passed, customer not arrived
        public const string STATUS_LATE = "Late";  // time  pass, customer not arrived
        public const string STATUS_WO = "IsWorkOrder";  // Changed to WO

        public const string STATUS_ARRIVED = "Arrived"; // customer arrived
        public const string STATUS_IN_PROGRESS = "In progress"; // stamping on progress
        public const string STATUS_INTERRUPTED = "Interrupted"; // stamping un-completed
        public const string STATUS_STAMP_DONE = "Done"; // stamping completed

        public const string STATUS_READY = "Ready"; // Workorder is ready
        public const string STATUS_READYFORINVOICE = "Handovered"; // Workorder is ready for invoicing
        public const string STATUS_INVOICED = "Invoiced"; // Workorder is invoiced
    
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }

        //format hh:ii
      //  public Decimal? duration { get; set; }
        public string Duration { get; set; }

        public DateTime? PickupDate; //date
        public DateTime? PickupTime; //time
        //GSAL.ESTDELTIME (the time)
        //public DateTime pickupDate { get; set; }

        //public string resource { get; set; }
        public string SmanId { get; set; }
        public string SName { get; set; }


        public int? WorkorderNo { get; set; }
        public string TransactionType { get; set; }
        public string TransactionNote { get; set; }

        public string LicenseNumber { get; set; }
        //VEHI.MAKE + ’ ’ + VEHI.MODEL + ’ ’ + VEHI.SUBMODEL
        //public string modelData { get; set; }
        public string Make { get; set; }
        public string Model { get; set; }
        public string SubModel { get; set; }

        //CUST.CUSTNO (customer number)
        //CUST.FNAME + ’ ’ + CUST.LNAME (first name first)
        public int? CustomerNo { get; set; }
        public string FName { get; set; }
        public string LName { get; set; }


        //GSAL.CONFNAME + ’ ’ + GSAL.CONLNAME
        public string ConFName { get; set; }
        public string ConLName { get; set; }
        public string ContactTel { get; set; }
        public string ContactEmail { get; set; }

        public string DeptId { get; set; }
        public string Dept { get; set; }
        public string StampStatus { get; set; }

        public string Type { get; set; }

        // public string RECEIVER { get; set; }

        // Status Of Reservation
        public int STAMID { get; set; }
        public DateTime? SERVDate { get; set; }
        public DateTime? WENDDate { get; set; }
        public DateTime? ENDDate { get; set; }
        public DateTime? WOREADYDATE { get; set; }
        public int CUSTWAIT { get; set; }
        public string WOSTATUS { get; set; }

        public string Status
        {
            get
            {
                var now = DateTime.Now;

                //invoiced
                if (this.WOSTATUS == "S") return STATUS_INVOICED;
                //ready for invoicing
                if (this.WOREADYDATE > DateTime.MinValue) return STATUS_READYFORINVOICE;

                //ready

                if (this.WENDDate != null)
                {
                    return STATUS_READY;
                }
                if (STAMID < 0)
                {
                    // NO Any record in STAM
                    if (this.TransactionType == "A")
                    {

                        if (now.CompareTo(this.StartTime) < 0)
                        {
                            return STATUS_WAITING;
                        }
                        else
                        {
                            return STATUS_LATE;
                        }
                    }
                    else
                    {
                        if (this.CUSTWAIT == 1) return STATUS_ARRIVED;
                        else return STATUS_WO;
                    }
                }
                else
                {
                    if (this.ENDDate != null)
                    {
                        if (StampStatus == "D")
                            return STATUS_STAMP_DONE;
                        else
                            return STATUS_INTERRUPTED;
                    }
                    else
                    {
                        return STATUS_IN_PROGRESS;
                    }
                }
            }
        }

        public string WorkDescription { get; set; }

        public Reservation(DateTime? startTime2, DateTime? endTime2, string durationStr, DateTime? pickupDate2,
            DateTime? pickupTime2, string smanId2, string sName2, int? workorderNo2, string transactionType2,
            string transactionNote2, string licenseNo2, string make2, string model2, string subModel2,
            int? customerNo2, string fName2, string lName2, string conFName2, string conLName2,
            string contactTel2, string contactEmail2, string workDescription2, string deptid)
        {
            StartTime = startTime2;
            EndTime = endTime2;
            Duration = durationStr;
            PickupDate = pickupDate2;
            PickupTime = pickupTime2;
            SmanId = smanId2;
            SName = sName2;
            WorkorderNo = workorderNo2;
            TransactionType = transactionType2;
            TransactionNote = transactionNote2;
            LicenseNumber = licenseNo2;
            Make = make2;
            Model = model2;
            SubModel = subModel2;
            CustomerNo = customerNo2;
            FName = fName2;
            LName = lName2;

            ConFName = conFName2;
            ConLName = conLName2;
            ContactTel = contactTel2;
            ContactEmail = contactEmail2;
            WorkDescription = workDescription2;

            // Dept = dept;
            DeptId = deptid == null ? "" : deptid;
        }

    }

}
